#include "Widget_Reader_TextBox.h"

#include <QtGui/QMouseEvent>
#include <QtGui/QTextBlock>
#include <QtGui/QMenu>

#include <QtCore/QDebug>

namespace Jonathan
{

   /**
    * @brief Initializes a new text box for the reader.
    *
    * @param parent Parent widget.
    */
   Widget_Reader_TextBox::Widget_Reader_TextBox (QWidget *parent) :
      QWebView (parent)
   {
      this->m_LinkedClicked = false;
      //this->setReadOnly (true);
      this->setContextMenuPolicy (Qt::CustomContextMenu);
      //this->setOpenLinks (true);
      //this->setOpenExternalLinks (false);
      this->page ()->setLinkDelegationPolicy (QWebPage::DontDelegateLinks);
      connect (this, SIGNAL (customContextMenuRequested (QPoint)), this, SLOT (ShowContextMenu (QPoint)));
      connect (this, SIGNAL (linkClicked (QUrl)), this, SLOT (LinkClicked (QUrl)));
   }

   /**
    * @brief Shows the custom context menu.
    *
    * @param point Point to show context menu at.
    */
   void Widget_Reader_TextBox::ShowContextMenu (QPoint point)
   {
      QMenu* menu = new QMenu (this);//this->createStandardContextMenu ();
      menu->addAction ("Search for " + this->selectedText ().split (' ').at (0) + "...");
      menu->exec (this->mapToGlobal (point));
      delete menu;
   }

   /*void Widget_Reader_TextBox::mouseReleaseEvent (QMouseEvent *event)
   {
      QTextEdit::mouseReleaseEvent (event);

      if (event->button () == Qt::RightButton)
      {
         if (this->textCursor ().hasSelection () || this->textCursor ().hasComplexSelection ())
            return;

         QTextCursor cursor2 = this->cursorForPosition (event->pos ());

         cursor2.movePosition (QTextCursor::StartOfWord);
         cursor2.movePosition (QTextCursor::EndOfWord, QTextCursor::KeepAnchor);

         this->setTextCursor (cursor2);
      }
   }*/

   void Widget_Reader_TextBox::LinkClicked (QUrl url)
   {
      stop ();
   }

}
